package com.yocard.yxyd.ui.presenter;

import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.webkit.ValueCallback;
import android.widget.RelativeLayout;

import com.yocard.yxyd.base.CommonSubscriber;
import com.yocard.yxyd.base.RxPresenter;
import com.yocard.yxyd.http.HttpManager;
import com.yocard.yxyd.http.model.request.CsmListRequest;
import com.yocard.yxyd.http.model.response.CsmOrderListBean;
import com.yocard.yxyd.others.ClassicsHeader;
import com.yocard.yxyd.ui.contract.CustomerContract;
import com.yocard.yxyd.utils.RxUtil;

import javax.inject.Inject;

/**
 * Created on 2019/4/8.
 */
public class CustomerPresenter extends RxPresenter<CustomerContract.View> implements CustomerContract.Presenter {
    int headHeight;
    @Inject
    public CustomerPresenter(HttpManager manager) {
        super(manager);
    }

    @Override
    public void fetchCsmList(int currentPage, CsmListRequest request) {
        addSubscribe(httpManager.getCsmOrderList(currentPage, request)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<CsmOrderListBean>(mView) {
                    @Override
                    public void onSuccess(CsmOrderListBean data) {
                        try {
                            mView.onFetcheList(data.getData());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }));
    }

    @Override
    public void setUpelasHead(RelativeLayout rlHeadLayout, ClassicsHeader classicsHeader) {
        rlHeadLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                headHeight = rlHeadLayout.getHeight();
                rlHeadLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        });

        ViewGroup.LayoutParams layoutParams = rlHeadLayout.getLayoutParams();
        ValueCallback<Integer> callback = new ValueCallback<Integer>() {
            @Override
            public void onReceiveValue(Integer value) {
                layoutParams.height = headHeight + value;
                rlHeadLayout.requestLayout();
            }
        };
        classicsHeader.setCallback(callback);
    }
}
